package com.jinghao.tws.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.tws.service.IWvAutoUploadCloudConfigService;
import com.jinghao.tws.service.IWvFlowService;
import com.jinghao.tws.util.EJhTwsCode;
import com.jinghao.tws.entity.WvAutoUploadCloudConfig;
import com.jinghao.tws.entity.WvAutoVedioConfig;
import com.jinghao.tws.entity.WvFlow;

@Service("wvAutoUploadCloudConfigService")
public class WvAutoUploadCloudConfigServiceImpl extends JingHaoBaseServiceImpl<WvAutoUploadCloudConfig> implements IWvAutoUploadCloudConfigService {
	//==================定制内容开始======================
	@Resource
	private IWvFlowService wvFlowService;
//在这里修改代码
	
	@Override
	public void listWvAutoUploadCloudConfig(WvAutoUploadCloudConfig model) {
		if (StringUtils.isEmpty(model.getFlowUuid())){
			throw new JingHaoCustomerException(EJhTwsCode.TWS9001.getCode(),"流程UUID不能为空，请重新输入!");
		}
		WvFlow wvFlow=new WvFlow();
		wvFlow.setFlowUuid(model.getFlowUuid());
		JhCnd cnd=new JhCnd();
		cnd.andEquals("flowUuid");
		wvFlow.setJhCnd(cnd);
		model.setPage(null);
		List<Map<String,Object>> list=this.wvFlowService.queryMapListById(wvFlow, "listWvFlowALLUserConfig");
		
		if(list==null||list.size()<=0){
			throw new JingHaoCustomerException(EJhTwsCode.TWS11001.getCode(),"未找到相关流程!");
		}else if(list.size()>1){
			throw new JingHaoCustomerException(EJhTwsCode.TWS11001.getCode(),"找到多条流程信息!");
		}
		Map<String,Object> tempMap=list.get(0);
		
		
		 model.setFlowCode(tempMap.get("code").toString());
		 cnd.reset();
		 cnd.andEquals("cc.fcode", "flowCode");
		 cnd.append(" and cc.process_id <> 'tally-event' ");
		 model.setJhCnd(cnd);
		 model.setPage(null);
		 List<Map<String,Object>> listConfig=this.queryMapListById(model, "listWvAutoUploadCloudConfigToCode");
		 
		 cnd.reset();
		 cnd.andEquals("flowCode");
		 model.setJhCnd(cnd);
		 List<Map<String,Object>> list2=this.queryMapList(model);
		 String processCode=null;
		 
		 List<String> otherProcess=new ArrayList();
		 otherProcess.add("demageProcess");
		 otherProcess.add("megerProcess");
		 /* otherProcess.add("camera");
		    otherProcess.add("flow"); */
		 
		 List<String> otherProcess2=new ArrayList();
		 otherProcess2.add("demageProcess");
		 otherProcess2.add("megerProcess");
         /* otherProcess2.add("camera");
		    otherProcess2.add("flow");*/
		 
		 Map<String,String> mapProcess=new HashMap<String,String>();
		 mapProcess.put("demageProcess","短损");
		 mapProcess.put("megerProcess","视频合并");  
		 
/*		 mapProcess.put("camera","摄像头信息");
		 mapProcess.put("flow","流程配置信息");*/
		 
		 Map<String,Object> newMap=null;
		 for(String process:otherProcess){
			 for(Map<String,Object> map:list2){
			     processCode=map.get("processCode").toString();
				 if(processCode.equals(process)){
					 newMap=new HashMap<String,Object>();
					 newMap.put("processCode", process);
					 newMap.put("name", mapProcess.get(process));
					 newMap.put("ctype", 1);
					 listConfig.add(newMap);
					 
					 otherProcess2.remove(process);
				 }
			 }
		 }
		 
		 for(String process:otherProcess2){
			 newMap=new HashMap<String,Object>();
			 newMap.put("processCode", process);
			 newMap.put("name", mapProcess.get(process));
			 newMap.put("ctype", 0);
			 listConfig.add(newMap);
		 }
		 tempMap.put("processList", listConfig);
		 model.setMapList(list);
		
	}

	@Override
	public void addWvAutoUploadCloudConfig(WvAutoUploadCloudConfig model) {
		if (StringUtils.isEmpty(model.getFlowCode())){
			throw new JingHaoCustomerException(EJhTwsCode.TWS11001.getCode(),"流程编码不能为空，请重新输入!");
		}
		if (StringUtils.isEmpty(model.getFlowUuid())){
			throw new JingHaoCustomerException(EJhTwsCode.TWS11001.getCode(),"流程UUID不能为空，请重新输入!");
		}
		JhCnd cnd=new JhCnd();
		cnd.andEquals("flowUuid");
		model.setJhCnd(cnd);
		this.deleteSame(model);
		Map<String,Object> map=model.getObjMap();
		WvAutoUploadCloudConfig wvAutoUploadCloudConfig=null;
		int i=0;
		if(map!=null){
			Set<String> set=map.keySet();
			for(String processCode:set){
				if(map.get(processCode)!=null&&map.get(processCode).toString().equals("true")){
					wvAutoUploadCloudConfig=new WvAutoUploadCloudConfig();
					wvAutoUploadCloudConfig.setApcUuid(model.getUUID());
					wvAutoUploadCloudConfig.setFlowUuid(model.getFlowUuid());
					wvAutoUploadCloudConfig.setFlowCode(model.getFlowCode());
					wvAutoUploadCloudConfig.setProcessCode(processCode);
					wvAutoUploadCloudConfig.setCreator(" ");
					wvAutoUploadCloudConfig.setCreateTime(new Date());
					wvAutoUploadCloudConfig.setSort(i++);
					this.add(wvAutoUploadCloudConfig);
				}
			}
		}
		
	}
	//==================定制内容结束======================
}
